﻿Imports KanBan.Pgd.Domain
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Infrastructure.Repositories
Imports KanBan.Pgd.Infrastructure.Notifications
Imports KanBan.Pgd.Domain.ValueObjects

Public Class SearchRequestService

    ''' <summary>
    ''' Asigna la Solicitud de Búsqueda a un usuario
    ''' </summary>
    ''' <param name="id">Id de la Solicitud de Búsqueda</param>
    ''' <param name="userId">Id del usurio que tomará esta Solicitud de Búsqueda</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function TakeSearchRequest(ByVal id As Guid, ByVal userId As Guid) As SearchRequest

        Dim searchRequestRepository As New SearchRequestRepository()
        Dim userRepository As New UserRepository()

        Dim request = searchRequestRepository.GetById(id)
        Dim responsible = userRepository.GetById(userId)

        request.Responsible = responsible
        request.SetStatus(SearchRequestStatus.InProgress)

        searchRequestRepository.SaveOrUpdate(request)

        Return request

    End Function

    ''' <summary>
    ''' Realiza la notificación a todos los usuarios del sistema acerca de una Solicitud de Búsqueda.
    ''' </summary>
    ''' <param name="id">Id de la Solicitud de Búsqueda</param>
    ''' <remarks></remarks>
    Public Sub PublishSearchRequest(ByVal id As Guid)

        Dim repository As New SearchRequestRepository()

        Dim request = repository.GetById(id)

        request.IsPublished = True

        'ToDo: Change this for more descriptive messages
        Notifier.NotifyAllUsers("New Search Request!", _
                                request.Description)

    End Sub

    ''' <summary>
    ''' Finaliza una Solicitud de Búsqueda
    ''' </summary>
    ''' <param name="id">Id de la Solicitud de Búsqueda</param>
    ''' <remarks></remarks>
    <Obsolete("Set Status directly instead")> _
    Public Sub FinalizeSearchRequest(ByVal id As Guid)

        Dim searchRequestRepository As New SearchRequestRepository()
        Dim request As SearchRequest = searchRequestRepository.GetById(id)

        request.SetStatus(SearchRequestStatus.Finished)

        searchRequestRepository.SaveOrUpdate(request)

    End Sub

    ''' <summary>
    ''' Devuelve todas las Solicitudes de Búsqueda en las que un usuario es Responsable
    ''' </summary>
    ''' <param name="userId"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetAllActiveByResponsible(ByVal userId As Guid) As IEnumerable(Of SearchRequest)

        Dim repository As New SearchRequestRepository()
        Return repository.GetAllActiveByResponsible(userId)

    End Function

    ''' <summary>
    ''' Devuelve todas las Solicitudes de Búsqueda en las que un usuario es Solicitante
    ''' </summary>
    ''' <param name="userId"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetAllActiveByRequester(ByVal userId As Guid) As IEnumerable(Of SearchRequest)

        Dim repository As New SearchRequestRepository()
        Return repository.GetAllActiveByRequester(userId)

    End Function

    ''' <summary>
    ''' Devuelve una Solicitud de Búsqueda por Id
    ''' </summary>
    ''' <param name="searchRequestId"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetById(ByVal searchRequestId As Guid) As SearchRequest

        Dim repository As New SearchRequestRepository()
        Return repository.GetById(searchRequestId)

    End Function

    Public Function GetAvailableSku() As String

        Dim repository As New SearchRequestRepository()
        Return repository.GetAvailableSku()

    End Function

    Public Sub SaveOrUpdate(ByVal request As SearchRequest)

        request.Sku = Me.GetAvailableSku()
        Dim repository As New SearchRequestRepository()
        repository.SaveOrUpdate(request)

    End Sub

    Public Function Search(ByVal profile As String, Optional ByVal requester As String = "", _
                           Optional ByVal responsible As String = "", Optional ByVal priorityId As String = "", _
                           Optional ByVal status As Integer = -1) As IEnumerable(Of SearchRequest)

        Dim repository As New SearchRequestRepository()
        Return repository.Query(profile, requester, responsible, priorityId, status)

    End Function

    Public Function SearchRequesters(ByVal text As String) As IList(Of String)

        Dim repository As New SearchRequestRepository()
        Return repository.QueryRequesters(text)

    End Function

    Public Function SearchProfiles(ByVal text As String) As IList(Of String)

        Dim repository As New SearchRequestRepository()
        Return repository.QueryProfiles(text)

    End Function

    Public Function Delete(ByVal searchRequestId As Guid) As Boolean

        Dim repository As New SearchRequestRepository()
        repository.DeleteLogical(searchRequestId)
        Return True

    End Function

    Public Sub SaveOrUpdateInterview(ByVal interview As Interview)

        Dim repository As New SearchRequestRepository()
        repository.SaveOrUpdateInterview(interview)

    End Sub

    Public Function DeleteInterview(ByVal interviewId As Guid) As Boolean

        Dim searchRequestRepository As New SearchRequestRepository()
        searchRequestRepository.DeleteInterviewLogical(interviewId)
        Return True

    End Function

End Class